package com.mis.service.impl.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mis.entity.biz.OrderMainEntity;
import com.mis.page.biz.OrderMainPage;
import com.mis.service.biz.OrderMainServiceI;
import com.mis.page.biz.OrderCustomerPage;
import com.mis.entity.biz.OrderCustomerEntity;

import com.jeecg.pageModel.TreeNode;
import com.jeecg.util.pojo.MyBeanUtils;
import com.jeecg.util.dbcommon.SearchSqlGenerateUtil;
import com.jeecg.service.impl.BaseServiceImpl;
import com.jeecg.pageModel.DataGrid;
import com.jeecg.dao.jdbc.JdbcDao;
import com.jeecg.dao.BaseDaoI;import com.mis.page.biz.OrderProductPage;
import com.mis.entity.biz.OrderProductEntity;

import com.jeecg.pageModel.TreeNode;
import com.jeecg.util.pojo.MyBeanUtils;
import com.jeecg.util.dbcommon.SearchSqlGenerateUtil;
import com.jeecg.service.impl.BaseServiceImpl;
import com.jeecg.pageModel.DataGrid;
import com.jeecg.dao.jdbc.JdbcDao;
import com.jeecg.dao.BaseDaoI;/**   
 * @Title: ServiceImpl
 * @Description: 订单表
 * @author zhangdaihao
 * @date 2014-12-08 17:21:54
 * @version V1.0   
 *
 */
@Service("orderMainService")
public class OrderMainServiceImpl extends BaseServiceImpl implements OrderMainServiceI {

	//SQL 使用JdbcDao
	@Autowired
	private JdbcDao jdbcDao;
	private BaseDaoI<OrderMainEntity> orderMainEntityDao;
	@Autowired
	private BaseDaoI<OrderCustomerEntity> orderCustomerEntityDao;
	@Autowired
	private BaseDaoI<OrderProductEntity> orderProductEntityDao;
	public BaseDaoI<OrderMainEntity> getOrderMainEntityDao() {
		return orderMainEntityDao;
	}
	@Autowired
	public void setOrderMainEntityDao(BaseDaoI<OrderMainEntity> orderMainEntityDao) {
		this.orderMainEntityDao = orderMainEntityDao;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public DataGrid datagrid(OrderMainPage orderMainPage) {
		DataGrid j = new DataGrid();
		j.setRows(getPagesFromEntitys(find(orderMainPage)));
		j.setTotal(total(orderMainPage));
		return j;
	}

	private List<OrderMainPage> getPagesFromEntitys(List<OrderMainEntity> orderMainEntitys) {
		List<OrderMainPage> orderMainPages = new ArrayList<OrderMainPage>();
		if (orderMainEntitys != null && orderMainEntitys.size() > 0) {
			for (OrderMainEntity tb : orderMainEntitys) {
				OrderMainPage b = new OrderMainPage();
				BeanUtils.copyProperties(tb, b);
				orderMainPages.add(b);
			}
		}
		return orderMainPages;
	}

	private List<OrderMainEntity> find(OrderMainPage orderMainPage) {
		String hql = "from OrderMainEntity t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderMainPage, hql, values);

		if (orderMainPage.getSort() != null && orderMainPage.getOrder() != null) {
			hql += " order by " + orderMainPage.getSort() + " " + orderMainPage.getOrder();
		}
		return orderMainEntityDao.find(hql, orderMainPage.getPage(), orderMainPage.getRows(), values);
	}

	private Long total(OrderMainPage orderMainPage) {
		String hql = "select count(*) from OrderMainEntity t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderMainPage, hql, values);
		return orderMainEntityDao.count(hql, values);
	}

	private String addWhere(OrderMainPage orderMainPage, String hql, List<Object> values) {
	    //循环查询条件Page的所有[Integer][String]类型的字段，如果字段不为空则动态加上查询条件
		//-----------------------------------------------------
		StringBuffer hqlbf = new StringBuffer(hql);
		OrderMainEntity orderMainEntity = new OrderMainEntity();
		BeanUtils.copyProperties(orderMainPage, orderMainEntity);
		SearchSqlGenerateUtil.createSearchParamsHql(hqlbf, values, orderMainEntity);
		hql = hqlbf.toString();
		//-----------------------------------------------------
		if (orderMainPage.getCcreatedatetimeStart() != null) {
			hql += " and createDt>=? ";
			values.add(orderMainPage.getCcreatedatetimeStart());
		}
		if (orderMainPage.getCcreatedatetimeEnd() != null) {
			hql += " and createDt<=? ";
			values.add(orderMainPage.getCcreatedatetimeEnd());
		}
		if (orderMainPage.getCmodifydatetimeStart() != null) {
			hql += " and modifyDt>=? ";
			values.add(orderMainPage.getCmodifydatetimeStart());
		}
		if (orderMainPage.getCmodifydatetimeEnd() != null) {
			hql += " and modifyDt<=? ";
			values.add(orderMainPage.getCmodifydatetimeEnd());
		}
		return hql;
	}

	public void add(OrderMainPage orderMainPage) {
		OrderMainEntity t = new OrderMainEntity();
		BeanUtils.copyProperties(orderMainPage, t);
		orderMainEntityDao.save(t);
	}

	
	/**
	 * 保存：一对多
	 */
	public void addMain(OrderMainPage orderMainPage,List<OrderCustomerPage> orderCustomerList,List<OrderProductPage> orderProductList)  throws Exception{
		//[1].主表保存
		OrderMainEntity t = new OrderMainEntity();
		BeanUtils.copyProperties(orderMainPage, t);
		orderMainEntityDao.save(t);
		//[2].明细数据保存
		//订单产品明细表
		for(OrderCustomerPage page:orderCustomerList){
			OrderCustomerEntity orderCustomer = new OrderCustomerEntity();
			BeanUtils.copyProperties(page, orderCustomer);
			
			//外键设置
			orderCustomer.setOrderObid(t.getObid());
			
			orderCustomerEntityDao.save(orderCustomer);
		}
		//订单产品明细
		for(OrderProductPage page:orderProductList){
			OrderProductEntity orderProduct = new OrderProductEntity();
			BeanUtils.copyProperties(page, orderProduct);
			
			//外键设置
			orderProduct.setOrderObid(t.getObid());
			
			orderProductEntityDao.save(orderProduct);
		}
	}
	/**
	 * 修改：一对多
	 */
	public void editMain(OrderMainPage orderMainPage,List<OrderCustomerPage> orderCustomerList,List<OrderProductPage> orderProductList)  throws Exception{
		//[1].主表保存
		OrderMainEntity t = orderMainEntityDao.get(OrderMainEntity.class, orderMainPage.getObid());
	    if(t != null) {
			MyBeanUtils.copyBeanNotNull2Bean(orderMainPage, t);
		}
	    
	    
	    //获取参数
		Object obid0 = orderMainPage.getObid();
		Object obid1 = orderMainPage.getObid();
	    
	    //-------------------------------------------------------------------
	    //[1].查询明细订单产品明细表
	    String hql0 = "from OrderCustomerEntity where 1 = 1 AND orderObid = ? ";
	    List<OrderCustomerEntity> orderCustomerOldList = orderCustomerEntityDao.find(hql0,obid0);
	    
	    //[2].删除明细订单产品明细表
		String delhql0 = "delete from OrderCustomerEntity where 1 = 1 AND orderObid = ? ";
		orderMainEntityDao.executeHql(delhql0,obid0);
	    //-------------------------------------------------------------------
	    //[1].查询明细订单产品明细
	    String hql1 = "from OrderProductEntity where 1 = 1 AND orderObid = ? ";
	    List<OrderProductEntity> orderProductOldList = orderProductEntityDao.find(hql1,obid1);
	    
	    //[2].删除明细订单产品明细
		String delhql1 = "delete from OrderProductEntity where 1 = 1 AND orderObid = ? ";
		orderMainEntityDao.executeHql(delhql1,obid1);
		//-------------------------------------------------------------------
		//[3].明细数据保存
		//订单产品明细表
		OrderCustomerEntity orderCustomerEntity = null;
		for(OrderCustomerPage page:orderCustomerList){
			for(OrderCustomerEntity c:orderCustomerOldList){
				if(c.getObid().equals(page.getObid())){
					orderCustomerEntity = c;
					break;
				}
			}
			//-----------------------------------------------------
			if(orderCustomerEntity==null){
				orderCustomerEntity = new OrderCustomerEntity();
			}
			orderCustomerEntityDao.evict(orderCustomerEntity);
			MyBeanUtils.copyBeanNotNull2Bean(page, orderCustomerEntity);
			//外键设置
			orderCustomerEntity.setOrderObid(t.getObid());
			    
			SearchSqlGenerateUtil.setUpdateMessage(orderCustomerEntity);
			orderCustomerEntityDao.save(orderCustomerEntity);
			orderCustomerEntity = null;
			//-----------------------------------------------------
		}
		//订单产品明细
		OrderProductEntity orderProductEntity = null;
		for(OrderProductPage page:orderProductList){
			for(OrderProductEntity c:orderProductOldList){
				if(c.getObid().equals(page.getObid())){
					orderProductEntity = c;
					break;
				}
			}
			//-----------------------------------------------------
			if(orderProductEntity==null){
				orderProductEntity = new OrderProductEntity();
			}
			orderProductEntityDao.evict(orderProductEntity);
			MyBeanUtils.copyBeanNotNull2Bean(page, orderProductEntity);
			//外键设置
			orderProductEntity.setOrderObid(t.getObid());
			    
			SearchSqlGenerateUtil.setUpdateMessage(orderProductEntity);
			orderProductEntityDao.save(orderProductEntity);
			orderProductEntity = null;
			//-----------------------------------------------------
		}
	}
	
	
	public void update(OrderMainPage orderMainPage) throws Exception {
		OrderMainEntity t = orderMainEntityDao.get(OrderMainEntity.class, orderMainPage.getObid());
	    if(t != null) {
			MyBeanUtils.copyBeanNotNull2Bean(orderMainPage, t);
		}
	}

	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				String hql = "from OrderMainEntity where obid = '"+id+"'";
				OrderMainEntity t = orderMainEntityDao.get(hql);
				if (t != null) {
					orderMainEntityDao.delete(t);
						//获取参数
					    Object obid0 = t.getObid();
					    Object obid1 = t.getObid();
					    //删除明细订单产品明细表
						String delhql0 = "delete from OrderCustomerEntity where 1 = 1 AND orderObid = ? ";
						orderMainEntityDao.executeHql(delhql0,obid0);
					    //删除明细订单产品明细
						String delhql1 = "delete from OrderProductEntity where 1 = 1 AND orderObid = ? ";
						orderMainEntityDao.executeHql(delhql1,obid1);
				}
			}
		}
	}

	public OrderMainEntity get(OrderMainPage orderMainPage) {
		return orderMainEntityDao.get(OrderMainEntity.class, orderMainPage.getObid());
	}

	public OrderMainEntity get(java.lang.String obid) {
		return orderMainEntityDao.get(OrderMainEntity.class, obid);
	}
	public List<OrderMainEntity> listAll(OrderMainPage orderMainPage) {
		String hql = "from OrderMainEntity where 1 = 1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderMainPage, hql, values);
		List<OrderMainEntity> list = orderMainEntityDao.find(hql,values);
		return list;
	}
	
	/**根据主表Key,查询子表明细：订单产品明细表*/
	public List<OrderCustomerPage> getOrderCustomerListByFkey(java.lang.String obid) {
		
		List<OrderCustomerPage> rs = new ArrayList<OrderCustomerPage>();
		String hql = "from OrderCustomerEntity where 1 = 1 AND orderObid = ? ";
		List<OrderCustomerEntity> list = orderCustomerEntityDao.find(hql,obid);
		
		for(OrderCustomerEntity po:list){
			OrderCustomerPage page = new OrderCustomerPage();
			BeanUtils.copyProperties(po, page);
			rs.add(page);
		}
		return rs;
	}
	/**根据主表Key,查询子表明细：订单产品明细*/
	public List<OrderProductPage> getOrderProductListByFkey(java.lang.String obid) {
		
		List<OrderProductPage> rs = new ArrayList<OrderProductPage>();
		String hql = "from OrderProductEntity where 1 = 1 AND orderObid = ? ";
		List<OrderProductEntity> list = orderProductEntityDao.find(hql,obid);
		
		for(OrderProductEntity po:list){
			OrderProductPage page = new OrderProductPage();
			BeanUtils.copyProperties(po, page);
			rs.add(page);
		}
		return rs;
	}
}
